﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CAGFIP.DataAccess.DataSetTableAdapters;
using CAGFIP.DataAccess;

namespace CAGFIP.Edicao
{
    public class RankingDAODebug : IRankingDAO
    {
        #region Fields

        private List<Ranking> rankings;
        private static readonly RankingDAODebug instance = new RankingDAODebug();

        #endregion
        
        #region Constructors

        private RankingDAODebug() { }

        #endregion

        #region Properties

        public static RankingDAODebug Instance
        {
            get { return instance; }
        }

        private List<Ranking> Rankings
        {
            get
            {
                if (this.rankings == null)
                {
                    //TODO: fill with preset debug assuntos
                    var rankings = new List<Ranking>();
                    rankings.Add(new Ranking("Facil", 0, "n1 f", 1));
                    rankings.Add(new Ranking("Facil", 1, "n1 f", 0.5));
                    rankings.Add(new Ranking("Facil", 2, "n1 f", 0));
                    rankings.Add(new Ranking("Medio", 0, "n1 m", 1));
                    rankings.Add(new Ranking("Medio", 1, "n1 m", 0.5));
                    rankings.Add(new Ranking("Medio", 2, "n1 m", 0));
                    rankings.Add(new Ranking("Dificil", 0, "n1 d", 1));
                    rankings.Add(new Ranking("Dificil", 1, "n1 d", 0.5));
                    rankings.Add(new Ranking("Dificil", 2, "n1 d", 0));
                    this.rankings = rankings;
                }
                return this.rankings;
            }
        }

        #endregion

        #region Methods

        public DataSet.RankingDataTable VisualizarRankingDataSet()
        {
            var r = new DataSet.RankingDataTable();

            foreach (var rk in Rankings)
                r.AddRankingRow(rk.Dificuldade, rk.Posicao, rk.Nome, rk.Pontuacao);

            return r;
        }

        public List<Ranking> VisualizarRanking()
        {
            var list = new List<Ranking>();

            foreach (var r in Rankings)
                list.Add(new Ranking(r.Dificuldade, r.Posicao, r.Nome, r.Pontuacao));

            return list;
        }

        public void AtualizarRanking(Ranking ranking)
        {
            int idx = Rankings.FindIndex(delegate(Ranking predicate)
            {
                return predicate.Dificuldade == ranking.Dificuldade && predicate.Posicao == ranking.Posicao;
            });

            if (idx == -1) throw new Exception("invalid");

            Rankings[idx] = ranking;
        }

        #endregion
    }
}
